<?php
/**
 * Magento
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Academic Free License (AFL 3.0)
 * that is bundled with this package in the file LICENSE_AFL.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/afl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@magento.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade Magento to newer
 * versions in the future. If you wish to customize Magento for your
 * needs please refer to http://www.magento.com for more information.
 *
 * @category    design
 * @package     default_default
 * @copyright   Copyright (c) 2006-2016 X.commerce, Inc. and affiliates (http://www.magento.com)
 * @license     http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
 */
?>
<script src="/js/echarts.min.js"></script>
<div id="main_chart" style="width: 1200px;height:500px;"></div>
<?php
$resource = Mage::getSingleton('core/resource');
$db = $resource->getConnection('core_read');

$collection = Mage::getModel('customer/customer')->getCollection()
            ->addAttributeToFilter('group_id', array('eq'=>4));

$days = array();
$today = date("Y-m-d");
for($i=7;$i>0;$i--)
{
    $days[] = date("Y-m-d", strtotime($today)-$i*86400);
}

$ids = array(0);
foreach($collection as $customer)
{
    $ids[] = $customer->getId();
}
$teams = implode(',', $ids);

$sql = "SELECT website_id,website_name FROM zgb_order GROUP BY website_id";
$query = $db->query($sql);
$rs = $query->fetchAll();
$regions = array();
$legend = array();
foreach($rs as $item)
{
    if($websiteId && $websiteId!=$item['website_id']) continue;
    $regions[$item['website_id']] = array('name'=>$item['website_name']);
    $legend[] = $item['website_name'];
}


foreach($regions as $key=>$data)
{
    foreach($days as $day)
    {
        $sql = "select sum(grand_total) from zgb_order where website_id='{$key}' and customer_id not in({$teams}) and left(created_at,10)='{$day}' and status in ('pending','processing','complete','complete_ship','complete_delivering','complete_order')";
        $query = $db->query($sql);
        $regions[$key]['order_total'][] = $query->fetchColumn();
        $query = null;
    }
}

$series = array();
foreach($regions as $key=>$region)
{
    $series[] = array(
            'name'=>$region['name'],
            'type'=>'bar',
            'data'=>$region['order_total']
        );
}

?>
<script type="text/javascript">
    // 基于准备好的dom，初始化echarts实例
    var myChart = echarts.init(document.getElementById('main_chart'));

    // 指定图表的配置项和数据
    var option = {
        title: {
            text: '交易额统计'
        },
        tooltip: {
            trigger:'axis'
        },
        legend: {
            data:<?php echo json_encode($legend);?>
        },
        xAxis: {
            type : 'category',
            data: <?php echo json_encode($days);?>
        },
        yAxis: [
            {
                type : 'value'
            }
        ],
        series: <?php echo json_encode($series);?>
    };

    // 使用刚指定的配置项和数据显示图表。
    myChart.setOption(option);
</script>

